<?php
/**
 * 用户评论列表等相关操作
 *
 */

class sms_model_comment extends Model {
	/**
	 * 每页显示数
	 *
	 */
	const PAGE_SIZE=25;
	/**
	 * 构造函数，初始化
	 *
	 */
	public function __construct(){
		parent::__construct();
		$this->tab_res = 'app_com_resources';
		$this->tab_thread = 'app_com_thread';
		$this->tab_post = 'app_com_post';
		$this->tab_mycomment = 'app_com_mycomment';
	}



	/**
	 * 根据用户ID获取用户的评论及评论回复资源id及相关信息
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 * @return unknown
	 */
	public function getCommentListByUid($uid,$args = array('app'=>'','control'=>'','action'=>'','page'=>1,'pageSize'=>10),$mycomment = false,$isMsg=false){
		if(!$uid || !is_numeric($uid)) return false;
		$page = $args['page']?$args['page']:1;
		$pageSize = $args['pageSize']?$args['pageSize']:10;
		$arr_list = $rid = array();
		$sql = 'SELECT * FROM '.$this->tab_mycomment.' r WHERE  ';//.($mycomment?('r.authorid='.$uid .' AND r.uid!='.$uid):('r.uid='.$uid.' AND r.authorid!='.$uid));
		$where = $mycomment?(' r.authorid='.$uid .' AND (r.uid!='.$uid.'  OR r.reply_me=1 )' ):('r.uid='.$uid.' AND r.authorid!='.$uid) ;
		$sql.=$where;
		if($isMsg==true){
			if($args['app'] && $args['control'] && $args['action']){ //查询某一类的评论
				$sql.=' AND r.app=\''.$args['app'].'\' AND r.control=\''.$args['control'].'\' AND act=\''.$args['action'].'\'';
			}
		}
		else{
			$sql.=' AND NOT (r.app=\'index\' AND r.control=\'index\' AND act=\'index\')';
		}
		if($mycomment==true){
			$sql.=' AND `type`=1 ';
		}
		$sql.='  ORDER BY up_time desc  ';
		$rst = $this->db->selectPage($sql, '', $page, $pageSize );
		if($rst['ROWS']){
			foreach ($rst['ROWS'] as $v){
				$arr_list[$v['thread_id']] = $v;
				
//				array_push($rid,$v['res_id']);
			}
		}
		$rst['ROWS'] = $arr_list;
		return $rst;
//		return array('rst'=>$rst,'rid'=>$rid);
	}
	/**
	 * 根据用户获取资源信息
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 * @return  array
	 */
	public function getResourceByUid($uid,$app='',$control='',$action=''){
		if(!$uid || !is_numeric($uid)) return false;
		$sql = 'SELECT res_id,res_name,uid,comment_num,app,control,act,id,create_time FROM '.$this->tab_res.' r WHERE r.uid='.$uid;
		if($app && $control && $action){
			$sql.=' AND r.app=\'index\' AND r.control=\'index\' AND act=\'index\'';
		}
		$sql.='AND r.comment_num>0 ORDER BY uptime desc  ';
		return  $this->db->selectPage($sql, '', $page, $pageSize );

	}
	/**
	 * 获取给定评论id的评论及回复信息
	 *
	 * @param unknown_type $tid
	 */
	/*public function getCommentByTid($tid){
		$tid = (is_array($tid) && !empty($tid))?implode($tid):$tid;
		$sql = 'SELECT p.thread_id,p.author_id,p.author_name,p.content,p.type,p.create_time FROM '.$this->tab_post.' p WHERE p.deleted=0 AND p.thread_id IN('.$tid.')  ';
		$rst = array();
		if($com = $this->db->fetchAll($sql)){
			foreach ($com as $v){
				$key = $com['type']==1?'comment':'recomment';//comment 为评论  recomment:为评论回复
				array_push($rst['thread_id'][$key][],array('author_id'=>$v['author_id'],'author_name'=>$v['author_name'],'content'=>$v['content'],'create_time'=>'create_time'));
			}
		}
	}*/
	
	
	/**
	 * 根据资源id获取资源所属用户id
	 *
	 * @param unknown_type $rid
	 * @return unknown
	 */
	public function getResourceOwner($tid){
		$sql = 'SELECT r.uid FROM '.$this->tab_res.' r,'.$this->tab_thread.' t WHERE t.thread_id = '.$tid.' AND  t.res_id=r.res_id';
		return  $this->db->fetchField($sql,'uid');
	}
	
	/**
	 * 获取参与资源评论的所有用户id
	 *
	 * @param unknown_type $rid
	 */
	public function getCommentUser($tid){
		$sql = 'SELECT p.author_id,p.type FROM app_com_post p WHERE p.thread_id=\''.$tid.'\'';
		/*$owner = array();
		$replyer = array();*/
		$userlist = array();
		if($rst = $this->db->fetchAll($sql)){
			foreach ($rst as $v){
				array_push($userlist,$v['author_id']);
				/*if ($v['type']==1) {
					array_push($owner);
				}
				else array_push($replyer);*/
			}
			return $userlist;
		}
		return false;
	}
	
	/**
	 * 用户发布评论及回复记录表
	 *
	 */
	public function upCommentLog($args = array('thread_id'=>'','uid'=>'','app'=>'','control'=>'','act'=>'','rid'=>'','res_id'=>'','authorid'=>''),$isComment = true){
		//		$args = filter_params($args,'res_id','uid','app','control','act','rid','authorid');
		trim_params($args);
		$type = $isComment?1:0;
		$reply_me = $args['uid']==$args['authorid']?0:1;
		$res_id = $args['res_id']?$args['res_id']:comment_util_comment::makeTheCode($args['app'],$args['control'],$args['act'],$args['rid']);
		$sql = 'INSERT INTO '.$this->tab_mycomment.' (thread_id,uid,app,control,act,rid,res_id,authorid,add_time,up_time,type) VALUES(\''.$args['thread_id'].'\',\''.$args['uid'].'\',\''.$args['app'].'\',\''.$args['control'].'\',\''.$args['act'].'\',\''.$args['rid'].'\',\''.$res_id.'\',\''.$args['authorid'].'\','.time().','.time().','.$type.') on DUPLICATE KEY UPDATE up_time = '.time();
		$this->db->query($sql);
		if(!$isComment){
			$sql = 'UPDATE '.$this->tab_mycomment.' a SET a. reply_me = 1 WHERE a.thread_id='.$args['thread_id'].' AND a.type=1 AND a.uid = a.authorid AND a.reply_me = 0 AND a.uid!='.$args['uid'];
			$this->db->query($sql);
			/*$sql = 'UPDATE '.$this->tab_mycomment.' a SET a. up_time = '.time().' WHERE a.thread_id='.$args['thread_id'].' AND a.type=1';
			return $this->db->query($sql);*/
		}
		$this->db->query('UPDATE '.$this->tab_mycomment.' a SET a. up_time = '.time().' WHERE a.thread_id='.$args['thread_id']);
		return true;
	}
	
	/**
	 * 删除用户发布评论及回复记录表
	 *
	 * @param unknown_type $tid  评论id
	 * @param unknown_type $uid   发评论id	
	 * @param unknown_type $type	0:表示回复 1：表示主题
	 * @return unknown
	 */
	
	public function delComment($tid,$uid,$type=1){
		if ($tid && $uid){
			if(is_array($tid))
				$tid = implode(',',$tid);
			$sql = 'DELETE FROM  app_com_mycomment  WHERE thread_id IN ('.$tid.') AND uid='.$uid;
			if(in_array($type,array(0,1))){
				$sql.=' AND type='.$type;
			}
			return $this->db->query($sql);
		}
		return ;
	}
	
	
	/**
	 * 获取评论列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $para  app:表示资源对应的app，control表示资源对应的control,actio:表示对应的action,page:当前页 pageSize:没页显示数
	 * @param unknown_type $myComment  TRUE 表示别人对我的评论，为FALSE表示我对别人的评论
	 * @return unknown
	 */
	public function getCommentList($uid,$para = array('app'=>'','control'=>'','action'=>'','page'=>1,'pageSize'=>10),$myComment=true){
		if(!$uid || !is_numeric($uid)) return false;
		$page = $para['page']?$para['page']:1;
		$pageSize = $para['pageSize']?$para['pageSize']:10;
		$colum = $myComment?'res_uid':'author_id';
		$commList = $arr_list = $tmp_list = array();
		$sql = 'SELECT a.thread_id,a.res_id,a.author_id,a.reply_time,a.create_time,a.app,a.control,a.act,a.res_uid,a.res_rid FROM '.$this->tab_thread.' a WHERE a.'.$colum.'='.$uid.' AND a.deleted=0';
		if($para['app'] && $para['control'] && $para['action']){ 
			$sql.=' AND a.app=\''.$para['app'].'\' AND a.control=\''.$para['control'].'\' AND a.act=\''.$para['action'].'\'';
		}
		$sql.=' ORDER BY a.reply_time DESC ';
		if($rst = $this->db->selectPage($sql, '', $page, $pageSize )){
			$arr_list = $rst['ROWS'];
			foreach ($arr_list as $v){
				$commList[$v['thread_id']] = $v;
				
			}
		}
		$rst['ROWS'] = $commList;
		return $rst;
	}
	
	/**
	 * 根据资源id或者主题id 获取评论及回复列表
	 *
	 * @param unknown_type $tid
	 * @param unknown_type $isResource   TRUE 表示源id，false：表示评论主题id
	 * @return unknown
	 */
	public function getRecommentListByRid($tid){
		$commList = $recommList = $arr_list = array();
//		$key = $isResource?'res_id':'thread_id';
		if(is_array($tid) && !empty($tid)) {
			$tid = implode(',',$tid);
		}
		$sql = 'SELECT a.id as post_id,a.thread_id,a.res_id,a.author_id,a.author_name,a.content,a.type,a.create_time FROM  '.$this->tab_post.' a WHERE a.thread_id IN('.$tid.') AND deleted=0 ORDER BY post_id ';
		if($rst = $this->db->fetchAll($sql)){
			foreach ($rst as $list){
				if ($list['type']==1) {
					$arr_list[$list['thread_id']]=$list;
				}else 
					$recommList[$list['thread_id']]['recoment'][] = $list;
			}
			reset($arr_list);
			if($recommList){
				foreach ($arr_list as $k=>$v){
					$commList[$k] = $v;
					if ($recommList[$k]['recoment']) {
						$commList[$k]['recoment'] = $recommList[$k]['recoment'];
					}
				}
			}else 
				$commList = $arr_list;
			unset($arr_list);
			return $commList;
		}
		return false;
	}
	
	/**
	 * 获取别人对我的评论列表
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $args
	 * @return unknown
	 */
	public function getInCommentList($uid,$args = array('app'=>'','control'=>'','action'=>'','page'=>1,'pageSize'=>10),$isMsg=false){
		if(!$uid || !is_numeric($uid)) return false;
		$args['page'] = $args['page']?$args['page']:1;
		$args['pageSize'] = $args['pageSize']?$args['pageSize']:10;
		$tids = $comlist = $com_list = $arr_list = $mycomlist =  array();
		$comlist = $this->getCommentListByUid($uid,$args,true,$isMsg);
		if($arr_list=$comlist['ROWS']){
			foreach ($arr_list as $v){
				array_push($tids,$v['thread_id']);
			}
			$mycomlist = $this->getRecommentListByRid($tids);
			reset($arr_list);
			foreach ($arr_list as $k=>$v){
				$com_list[$k] = $v;
				$resType = sms_util_comment::getResourceTypeName($v['app'],$v['control'],$v['act']);
				$num = comment_util_comment::getCommentCount($v['app'],$v['control'],$v['act'],$v['rid']);//获取评论数
				$com_list[$k]['com_num'] = $num;
				$com_list[$k]['author_id'] = $mycomlist[$k]['author_id'];
				$com_list[$k]['author_name'] = $mycomlist[$k]['author_name'];
//				if($mycomlist[$k]['recoment'])
				$com_list[$k]['recoment'] = $mycomlist[$k]['recoment']?$mycomlist[$k]['recoment']:'';
				$com_list[$k]['content'] = $mycomlist[$k]['content'];
				$com_list[$k]['type'] = $mycomlist[$k]['type'];
				$com_list[$k]['add_time'] = $mycomlist[$k]['create_time'];
				$com_list[$k]['typename'] = $resType['name'];
				$com_list[$k]['classname'] = $resType['callback'];
			}
//			sms_util_comment::sasort($com_list,'up_time','desc');
			$comlist['ROWS'] = $com_list;
			unset($com_list);unset($arr_list);
		}
		return $comlist;
	}
	
	/**
	 * 获取我对别人的评论及回复
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $args
	 */
	public function getOutCommentList($uid,$args = array('app'=>'','control'=>'','action'=>'','page'=>1,'pageSize'=>10),$isMsg=false){
		if(!$uid || !is_numeric($uid)) return false;
		$args['page'] = $args['page']?$args['page']:1;
		$args['pageSize'] = $args['pageSize']?$args['pageSize']:10;
		$tids = $comlist = $com_list = $arr_list = $mycomlist =  array();
		$rsid = '';
		//获取我评论的资源列表
		$comlist = $this->getCommentListByUid($uid,$args,false,$isMsg);
		if($arr_list=$comlist['ROWS']){
			foreach ($arr_list as $v) {
				array_push($tids,$v['thread_id']);
			}
		}
		$mycomlist = $this->getRecommentListByRid($tids);
		reset($arr_list);
		foreach ($arr_list as $k=>$v){
			$com_list[$k] = $v;
			$resType = sms_util_comment::getResourceTypeName($v['app'],$v['control'],$v['act']);
			$num = comment_util_comment::getCommentCount($v['app'],$v['control'],$v['act'],$v['rid']);//获取评论数
			$com_list[$k]['com_num'] = $num;
			$com_list[$k]['author_id'] = $mycomlist[$k]['author_id'];
			$com_list[$k]['author_name'] = $mycomlist[$k]['author_name'];
//			if($mycomlist[$k]['recoment'])
			$com_list[$k]['recoment'] = $mycomlist[$k]['recoment']?$mycomlist[$k]['recoment']:'';
			$com_list[$k]['content'] = $mycomlist[$k]['content'];
			$com_list[$k]['type'] = $mycomlist[$k]['type'];
			$com_list[$k]['add_time'] = $mycomlist[$k]['create_time'];
			$com_list[$k]['typename'] = $resType['name'];
			$com_list[$k]['classname'] = $resType['callback'];
		}
//		sms_util_comment::sasort($com_list,'up_time','desc');
		$comlist['ROWS'] = $com_list;
		return $comlist;
	}
}
?>